package com.zhilianbao.leyaogo.ui.adapter.home;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.zhilianbao.leyaogo.R;

import java.util.List;

/**
 * ClassName: SearchHistoryAdapter <br>
 * Description: 搜索历史Adapter<br>
 * Author: Cyarie <br>
 * Created: 2016/11/2 14:22 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class SearchHistoryAdapter extends SwipeMenuAdapter<SearchHistoryAdapter.SearchHistoryViewHolder> {

    private List<String> mList;

    public SearchHistoryAdapter(List<String> list) {
        mList = list;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_history, parent, false);
    }

    @Override
    public SearchHistoryAdapter.SearchHistoryViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new SearchHistoryAdapter.SearchHistoryViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(SearchHistoryAdapter.SearchHistoryViewHolder holder, int position) {
        holder.setData(mList.get(position));
        holder.tvHistory.setOnClickListener(v -> {
            if (mOnHistoryItemClickListener != null)
                mOnHistoryItemClickListener.onHistoryItemCLick(mList.get(position));

        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    public static class SearchHistoryViewHolder extends RecyclerView.ViewHolder {
        public TextView tvHistory;


        public SearchHistoryViewHolder(View itemView) {
            super(itemView);
            tvHistory = (TextView) itemView.findViewById(R.id.tv_history);
        }

        public void setData(String history) {
            this.tvHistory.setText(history);
        }
    }

    public interface OnHistoryItemClickListener {
        void onHistoryItemCLick(String content);
    }

    private OnHistoryItemClickListener mOnHistoryItemClickListener;


    public void setOnHistoryItemClickListener(OnHistoryItemClickListener mOnHistoryItemClickListener) {
        this.mOnHistoryItemClickListener = mOnHistoryItemClickListener;
    }

}
